import json
import random
from ronglian_sms_sdk import SmsSDK

from configs.sms_config import accId, accToken, appId, SUCCESS_CODE, tid, SMS_VALIDATE
from configs import config, const
from utils import util

sdk = SmsSDK(accId, accToken, appId)


# mobile = '手机号1,手机号2'
# datas = ('变量1', '变量2')
# 免费开发测试使用的模板ID为1，具体内容：【云通讯】您的验证码是{1}，请于{2}分钟内正确输入。其中{1}和{2}为短信模板参数。

def getSMSKey(mobile, prefix=const.redis_prefix_mall_login):
    return prefix + str(mobile)


async def send_message(mobile, prefix):
    code = msg_code()
    resp = util.json_decode(sdk.sendMessage(tid, mobile, (code, SMS_VALIDATE)))
    status_code = resp.get("statusCode")
    if status_code != SUCCESS_CODE:
        # TODO
        return False
    else:
        await config.redis.setex(getSMSKey(mobile, prefix), SMS_VALIDATE * 60, code)
        return True


def msg_code():
    sms_code = '%06d' % random.randint(0, 999999)
    print(sms_code)
    return sms_code
